package com.ai.module.chat.common.base;

import com.ai.module.chat.common.constant.RedisKeyStringConstant;
import com.ai.module.chat.common.constant.StringConstant;
import com.ai.module.chat.config.advice.exception.BusinessException;
import com.ai.module.user.domain.vo.UserLoginInfo;
import com.ai.module.chat.util.DateUtils;
import com.ai.module.chat.util.RedisTemplateUtils;
import com.baomidou.mybatisplus.core.toolkit.StringPool;
import com.baomidou.mybatisplus.core.toolkit.StringUtils;
import lombok.Setter;
import lombok.extern.slf4j.Slf4j;
import org.springframework.web.context.request.RequestContextHolder;
import org.springframework.web.context.request.ServletRequestAttributes;

import javax.servlet.http.HttpServletRequest;
import java.util.Map;
import java.util.concurrent.ConcurrentHashMap;

/**
 * @author: MrHe
 * @create: 2023-09-02 11:47
 * @description:
 **/
@Slf4j
public class ThreadLocalContext extends ThreadLocal<String>{

   static Map<String,Object> userInfo = new ConcurrentHashMap<>();

   @Setter
   private static UserLoginInfo userLoginInfo;

    public static void set(String key,Object vale){
        userInfo.put(key,vale);
    }

    public  static Object get(String key){
        return userInfo.get(key);
    }

    public static void remove(String key){
        userInfo.remove(key);
    }

    public static void clear(){
        userInfo.clear();
        userLoginInfo=null;
    }

    public static String getUserId() {
        if (userLoginInfo ==null){
            return StringPool.EMPTY;
        }
        return userLoginInfo.getUuid();
    }
    public static void  initUserInfo(UserLoginInfo userLoginInfo){
        String token = (String) get(StringConstant.TOKEN);
        if (StringUtils.isBlank(token)){
            throw new BusinessException("用户数据确实,请待会再试");
        }
        String userTokenKey = String.format(RedisKeyStringConstant.USER_INFO, token);
        RedisTemplateUtils.set(userTokenKey, userLoginInfo, DateUtils.WEK_DAY);
    }

    public static void initPoint(String loginToken, int realPoint) {
        String userTokenKey = String.format(RedisKeyStringConstant.USER_INFO, loginToken);
        UserLoginInfo userLoginInfo =  RedisTemplateUtils.get(userTokenKey);
        userLoginInfo.setPoints(realPoint);
        RedisTemplateUtils.set(userTokenKey, userLoginInfo, DateUtils.WEK_DAY);
    }

    public static UserLoginInfo getUserLoginInfo() {
        if (userLoginInfo ==null){
            log.error("获取的 getUserLoginInfo 为  null"  );
            try {
                // 获取 用户 requets
                ServletRequestAttributes attributes = (ServletRequestAttributes) RequestContextHolder.getRequestAttributes();
                if (attributes==null || attributes.getRequest()==null){
                    log.error("用户登录失败,attributes [{}]",attributes);
                    return null;
                }
                HttpServletRequest request = attributes.getRequest();

                String requestURI = request.getRequestURI();
                String headerToken = request.getHeader(StringConstant.TOKEN);
                if (org.springframework.util.StringUtils.isEmpty(headerToken)){
                    log.error("用户登录失败,headerToken [{}],requestURI [{}]",headerToken,requestURI);
                    return null;
                }
                UserLoginInfo userLogin = RedisTemplateUtils.get(String.format(RedisKeyStringConstant.USER_INFO, headerToken));
                log.error("用户登录失败,但是重新从请求头获取数据成功, UserLoginInfo [{}] ,headerToken [{}],requestURI [{}]",userLogin,  headerToken,requestURI);
                return userLogin;
            }catch (Exception ex){
                ex.printStackTrace();
            }
        }
        return userLoginInfo;
    }

    public static void setUserLoginInfo(UserLoginInfo userLoginInfo) {
        if (userLoginInfo ==null){
            log.error("设置的 userLoginInfo 为 userLoginInfo[{}] ", getUserLoginInfo() );
            try {
                userLoginInfo.getId();
            }catch (Exception ex){
                ex.printStackTrace();
            }
        }
        ThreadLocalContext.userLoginInfo = userLoginInfo;
    }
}
